<?php
namespace admin\action;

use herosphp\bean\Beans;
use herosphp\http\HttpRequest;
use herosphp\utils\ArrayUtils;

/**
 * user_log action
 * @package admin\action
 * @author yangjian<yangjian102621@gmail.com>
 */
class UserLogAction extends CommonAction {

    protected $serviceBean = 'user.userLog.service';
    protected $actionTitle = '用户资金流水';

    //资金流水
    public function index(HttpRequest $request) {

        $serial_no = $request->getParameter('serial_no', 'trim');
        $username = $request->getParameter('username', 'trim');
        $order_no = $request->getParameter('order_no', 'trim');
        $app_id = $request->getParameter('app_id', 'trim');
        $type = $request->getParameter('type', 'trim');
        $starttime = $request->getParameter('starttime', 'trim');
        $endtime = $request->getParameter('endtime', 'trim');

        $conditions = array();
        if ( $serial_no ) $conditions['serial_no'] = "%{$serial_no}%";
        if ( $username ) $conditions['username'] = "%{$username}%";
        if ( $order_no ) $conditions['order_no'] = "%{$order_no}%";
        if ( $app_id ) $conditions['app_id'] = $app_id;
        if ( $type ) $conditions['type'] = $type;
        if ( $starttime ) $conditions['addtime']['>'] = $starttime.' 00:00:00';
        if ( $endtime ) $conditions['addtime']['<'] = $endtime.' 24:00:00';

        $this->setConditions($conditions);
        $this->assign('logTypes', array(
            'inpay' => '入金',
            'outpay' => '出金',
            'cash' => '提现',
            'pay' => '支付',
            'confirm' => '确认收货'
        ));

        $this->assign('tableHeader', array('流水号','大塑账户','发生额(￥)','账户余额(￥)','资金类型','业务类型','具体业务', '交易时间'));
        if( $request->getParameter('__export') == 1 ) {
            $this->export($request); //导出数据

        } else {

            parent::index($request);
            $this->setView('user/user_log_index');
            $this->assign('breadcrumb', array('支付管理', '资金流水'));

        }

    }

    //加载银行
    protected function loadBanks() {
        $service = Beans::get('payment.bank.service');
        $banks = $service->getItems(array('status' => 1));
        $banks = ArrayUtils::changeArrayKey($banks, 'id');
        $this->assign('banks', $banks);
    }

    //导出数据
    protected function export(HttpRequest $request) {

        if( $request->getParameter('__export') == 1 ){
            $ids = $request->getParameter('ids');
            if( $ids ){
                $conditions['id']['$in']=$ids;
                $this->setConditions($conditions);
            }
            $this->setPagesize(getConfig('export_max_count'));
            $excel = array();
            $service = Beans::get($this->getServiceBean());
            $items = $service->where($this->getConditions($conditions))->limit($this->getPage(), $this->getPagesize())->sort(array('addtime' => -1))->find();
            foreach($items as $k => $v)
            {
                $tmp = array();
                $tmp[] = $v["serial_no"];
                $tmp[] = $v['username'];
                $tmp[] = $v["amount"];
                $tmp[] = $v["usable"];
                if ( $v['atype'] == '+' ) {
                    $tmp[] = '收入';
                } elseif ( $v['atype'] == '-' ) {
                    $tmp[] = '支出';
                } else {
                    $tmp[] = '---';
                }
                $logTypes = $this->getTemplateVar('logTypes');
                $tmp[] = $logTypes[$v['type']];
                $tmp[] = '订单号：'.$v['order_no'];
                $tmp[] = $v['addtime'];
                $excel[] = $tmp;
            }
            $title = '资金流水';
            $header = $this->getTemplateVar('tableHeader');
            phpexcel($title,$header,$excel);
            exit();
        }
    }

}
